package com.heimo.babyage.ui.dialogcolor;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.heimo.babyage.R;
import com.larswerkman.colorpicker.ColorPicker;
import com.larswerkman.colorpicker.ColorPicker.OnColorChangedListener;
import com.larswerkman.colorpicker.OpacityBar;
import com.larswerkman.colorpicker.SVBar;
import com.larswerkman.colorpicker.SaturationBar;
import com.larswerkman.colorpicker.ValueBar;

public class ColorDialog extends Dialog implements OnColorChangedListener {
	private Context ctx;
	private int defaultColor = 0;
	private OnColorChangedListener colorListener = null;

	public ColorDialog(Context ctx, int defaultColor,
			OnColorChangedListener colorListener) {
		super(ctx);
		this.ctx = ctx;
		this.setTitle(ctx.getString(R.string.lab_text_color));
		this.defaultColor = defaultColor;
		this.colorListener = colorListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_layout_colors);
		init();
	}

	private void init() {
		final ColorPicker picker = (ColorPicker) findViewById(R.id.picker);
		SVBar svBar = (SVBar) findViewById(R.id.svbar);
		OpacityBar opacityBar = (OpacityBar) findViewById(R.id.opacitybar);
		SaturationBar saturationBar = (SaturationBar) findViewById(R.id.saturationbar);
		ValueBar valueBar = (ValueBar) findViewById(R.id.valuebar);

		picker.addSVBar(svBar);
		picker.addOpacityBar(opacityBar);
		picker.addSaturationBar(saturationBar);
		picker.addValueBar(valueBar);

		// To set the old selected color u can do it like this
		picker.setOldCenterColor(defaultColor);
		// adds listener to the colorpicker which is implemented
		// in the activity
		picker.setOnColorChangedListener(this);
		
		Button btn = (Button) findViewById(R.id.btn_close_color_dialog);
		Typeface tf = Typeface.createFromAsset(ctx.getAssets(),"fonts/orbitron-bold.otf");
		if(tf!=null){
			btn.setTypeface(tf);					
		}
		btn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int newColor = picker.getColor();
				colorListener.onColorChanged(newColor);
				ColorDialog.this.dismiss();
			}
		});
	}

	/*
	 * OnColorChangedListener
	 */
	@Override
	public void onColorChanged(int color) {

	}

}
